import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:untitled/pages/dio_net/user_bean.dart';


class Dioget extends StatefulWidget {
  const Dioget({Key? key}) : super(key: key);

  @override
  State<Dioget> createState() => _DiogetState();
}

class _DiogetState extends State<Dioget> {
  String result = "";
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("网络请求"),
      ),
      body: Row(
        children: [
          Expanded(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                FlatButton(
                  onPressed:(){
                    getRequestFunction1();
                  } , 
                  child: Text("发起请求")
                  ),
                  Text("请求的数据$result",maxLines: 10,),
                  //Text("用户的姓名时${mUserBean!=null?mUserBean?.userName:""}")
              ],
            ),
          )
        ],
      ),
    );
    
  }
  //get请求
  UserBean? mUserBean;
void getRequestFunction1() async {
    Dio dio = new Dio();
    int userId = 1;
    // 请求地址
    String url = "http://jdmall.itying.com/api/plist?is_hot=1";
    String url2 = "http://jdmall.itying.com/api/plist?is_hot=$userId";
    String url3 = "http://jdmall.itying.com/api/plist";

    Map<String,dynamic> map = Map();
    map["userId"] = userId;
    //Response response = await dio.get(url3,queryParameters: map);
    Response response = await dio.get(url);

    // 响应数据
    var data = response.data;
    // 响应数据
    Map<String,dynamic> data3 = response.data;
    // 解析json
    UserBean userBean = UserBean.fromJson(data3);

    setState(() {
      //mUserBean = userBean;
      result = data.toString();
    });

  }
  // post请求 并提交FormData参数
  void postRequestFunction() async{
    String url = "https://suggest.taobao.com/sug?code=utf-8&q=%E5%8D%AB%E8%A1%A3&callback=cb";
    // 创建Dio
    Dio dio = new Dio();
    // 创建Map 封装参数
    Map<String,dynamic> map = Map();
    map['userName'] = "小明";
    map['userAge'] = 44;
    // 创建FormData
    FormData formData = FormData.fromMap(map);
    // 发起post请求
    Response response = await dio.post(url,data: formData);

    var data = response.data;
  }

  // post请求 发送json
  void postRequestFunction2() async{
    String url = "https://suggest.taobao.com/sug?code=utf-8&q=%E5%8D%AB%E8%A1%A3&callback=cb";
    // 创建Dio
    Dio dio = new Dio();
    // 创建Map 封装参数
    Map<String,dynamic> map = Map();
    map['userName'] = "小明";
    map['userAge'] = 44;
    // 创建FormData
  
    // 发起post请求
    Response response = await dio.post(url,data: map);

    var data = response.data;
  }
  //Flutter网络请求之post上传文件
  String localImagePath = "/storage/emulated/0/Download/17306285.jpg";
  // 上传的服务器地址
  String netUploadUrl = "http://192.168.0.102.8080/fileupload";

  void fileUpload() async{
    Dio dio = new Dio();
    Map<String, dynamic> map = Map();
    map['auth'] = "12345";
    map['file'] = await MultipartFile.fromFile(localImagePath,filename: "xxx.png");
    FormData formData = FormData.fromMap(map);
    Response response = await dio.post(netUploadUrl,data: formData,onSendProgress: (int progress,int total){
      print('当前进度是$progress 总进度是$total');
    });
    var data = response.data;
  }


}